package cn.sxt.users.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

import cn.sxt.common.util.ConstatFinalUtil;
import cn.sxt.common.util.PageInfoUtil;
import cn.sxt.users.dao.IAAdminsDao;
import cn.sxt.users.dao.IAMenuDao;
import cn.sxt.users.dao.IARoleDao;
import cn.sxt.users.pojo.AAdmins;
import cn.sxt.users.pojo.AMenu;
import cn.sxt.users.pojo.ARole;
import cn.sxt.users.service.IUsersService;

/**
 * 和用户相关的Service实现类
 * @Service("usersService")
 * @Resource
 * ===
 * <bean id="usersService" class="cn.sxt.users.service.impl.UsersServiceImpl">
 * 	<property name="adminsDao" ref="adminsDao">
 * </bean>
 * 
 * @author wangshSxt
 *
 */
@Service("usersService")
public class UsersServiceImpl implements IUsersService
{
	@Resource
	private IAAdminsDao adminsDao;
	@Resource
	private IARoleDao roleDao;
	@Resource
	private IAMenuDao menuDao;
	
	@Override
	public AAdmins findOneAdminsService(Map<String, Object> condMap)
	{
		return this.adminsDao.findOneDao(condMap);
	}

	/**
	 * {"code":"0","info":"","data":{"id":""}}
	 * 拼装JSON和解析JSON
	 * 凡是见到大括号就JSONObject,凡是[],就是JSON数组
	 */
	@Override
	public JSONObject saveOneAdminsService(AAdmins admins)
	{
		JSONObject resultJSON = new JSONObject();
		
		/* 验证邮箱不能重复 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("email", admins.getEmail());
		AAdmins adminsRes = this.findOneAdminsService(condMap);
		if(adminsRes != null)
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", "邮箱已经存在,请更换;id:" + adminsRes.getId());
			return resultJSON ; 
		}
		
		int res = this.adminsDao.save(admins);
		if(res > 0 )
		{
			/* 保存成功 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", res);
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			/* 保存失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneAdminsService(AAdmins admins)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.adminsDao.update(admins);
		/* 更新角色 */
		if(res > 0 )
		{
			/* 保存成功 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", res);
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			/* 保存失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public List<AAdmins> findCondListAdminsService(PageInfoUtil pageInfoUtil , Map<String, Object> condMap)
	{
		return this.adminsDao.findCondListDao(pageInfoUtil,condMap);
	}
	
	@Override
	public ARole findOneRoleService(Map<String, Object> condMap)
	{
		return this.roleDao.findOneDao(condMap);
	}

	/**
	 * {"code":"0","info":"","data":{"id":""}}
	 * 拼装JSON和解析JSON
	 * 凡是见到大括号就JSONObject,凡是[],就是JSON数组
	 */
	@Override
	public JSONObject saveOneRoleService(ARole role)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.roleDao.save(role);
		if(res > 0 )
		{
			/* 保存成功 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", res);
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			/* 保存失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneRoleService(ARole role)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.roleDao.update(role);
		/* 更新角色 */
		if(res > 0 )
		{
			/* 保存成功 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", res);
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			/* 保存失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public List<ARole> findCondListRoleService(PageInfoUtil pageInfoUtil , Map<String, Object> condMap)
	{
		return this.roleDao.findCondListDao(pageInfoUtil,condMap);
	}

	@Override
	public AMenu findOneMenuService(Map<String, Object> condMap)
	{
		return this.menuDao.findOneDao(condMap);
	}

	/**
	 * {"code":"0","info":"","data":{"id":""}}
	 * 拼装JSON和解析JSON
	 * 凡是见到大括号就JSONObject,凡是[],就是JSON数组
	 */
	@Override
	public JSONObject saveOneMenuService(AMenu menu)
	{
		JSONObject resultJSON = new JSONObject();
		
		int res = this.menuDao.save(menu);
		if(res > 0 )
		{
			/* 保存成功 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", res);
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			/* 保存失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneMenuService(AMenu menu)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.menuDao.update(menu);
		/* 更新角色 */
		if(res > 0 )
		{
			/* 保存成功 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", res);
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else
		{
			/* 保存失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public List<AMenu> findCondListMenuService(PageInfoUtil pageInfoUtil , Map<String, Object> condMap)
	{
		return this.menuDao.findCondListDao(pageInfoUtil,condMap);
	}

	/**
	 * 如果menu为null,表示查询所有的一级节点(菜单)
	 */
	@Override
	public String menuTreeService(AMenu menu)
	{
		StringBuffer sb = new StringBuffer();
		sb.append("<ul class='tree'>");
		if(menu == null)
		{
			/* 查询所有的一级节点 parentId=null*/
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("parentIdCond", "true");
			List<AMenu> menuList = this.findCondListMenuService(null, condMap);
			for (Iterator iterator = menuList.iterator(); iterator.hasNext();)
			{
				AMenu menuFirst = (AMenu) iterator.next();
				this.menuTreeServiceUtil(sb, menuFirst);
			}
		}else
		{
			/* 查询当前菜单向下的树形结构 */
			this.menuTreeServiceUtil(sb,menu);
		}
		sb.append("</ul>");
		return sb.toString();
	}
	
	/**
	 * 树形结构的递归
	 * 
	 * <ul class='tree'>
			<li>
				<a href='tabsPage.html' target='navTab'>框架面板</a>
				<ul>
					<li><a href='main.html' target='navTab' rel='main'>我的主页</a></li>
					<li><a href='newPage1.html' target='navTab' rel='page1'>页面一</a></li>
				</ul>
			</li>
			<li><a href='w_panel.html' target='navTab' rel='w_panel'>面板</a></li>
		</ul>
	 */
	void menuTreeServiceUtil(StringBuffer sb ,AMenu menu)
	{
		sb.append("<li>");
		sb.append("	<a href='tabsPage.html' target='navTab'>"+ menu.getName() +"</a>");
		
		/* 根据当前id查询子菜单 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.clear();
		condMap.put("parentIdCond", "true");
		condMap.put("parentId", menu.getId());
		List<AMenu> menuList = this.findCondListMenuService(null, condMap);
		ConstatFinalUtil.LOGGER.error("---名称:{},子节点大小:{},条件:{}--", menu.getName() , menuList.size(),condMap);
		
		if(menuList.size() > 0)
		{
			sb.append("<ul>");
			for (Iterator iterator = menuList.iterator(); iterator.hasNext();)
			{
				AMenu menuChild = (AMenu) iterator.next();
				this.menuTreeServiceUtil(sb, menuChild);
			}
			sb.append("</ul>");
		}
		
		sb.append("</li>");
	}
}
